<?php

require_once 'FWidget.class.php';

class FLookup extends FWidget
{
    private $itemId;
    
    private $itemDesc;
    
    private $filters;
    
    private $searchForm;
    
    private $frmHeight = '500';
    
    private $frmWidth = '700';
    
    private $frmGridColumns;
    
    public function __construct($name, $fieldLabel = '', $itemId = 'codigo', $itemDesc = 'descricao', $fieldWidth = 20, $readOnly = false)
    {
        parent::__construct($name, 'flookup', $fieldLabel, $fieldWidth, $readOnly);
        $this->setItemId($itemId);
        $this->setItemDesc($itemDesc);
        $this->setIsInput(true);
    }
    
    public function generate()
    {
        if ( !$this->getSearchForm() )
        {           
            $model = new FDefaultModel($this->getTable());
            
            $form = $model->getForm();
            $form->setTitle($this->getFieldLabel());
            $form->setTableName($this->getTable());
            $form->setHeight($this->getFrmHeight());
            $form->setWidth($this->getFrmWidth());
            $form->setMaximized(false);
            $form->setIsLookupFieldForm(true);
            $form->generate();
            
            $this->setSearchForm($form->getName());
        }
        
        $allowBlank = $this->required ? 'false' : 'true';
        $storeFields = "'" . $this->getItemId() . "','" . $this->getItemDesc() . "'";

        return "{
            xtype: '{$this->getXtype()}',
            name: '{$this->getName()}', 
            descFieldName: '{$this->getItemDesc()}',
            fieldLabel: '{$this->getFieldLabel()}',
            fieldWidth: {$this->getFieldWidth()},
            table: '{$this->getTable()}',
            storeFields: [$storeFields],
            dataIndex: '{$this->getItemId()}',
            dataDesc: '{$this->getItemDesc()}',
            className: '{$this->getClassName()}',
            searchForm: '{$this->getSearchForm()}',
            formWidth: {$this->getFrmWidth()},
            formHeigth: {$this->getFrmHeight()},
            allowBlank: $allowBlank
        }";
    }
    
    public function addFilter($filter)
    {
        $this->filters[] = $filter;
    }

    public function getFilters()
    {
        return $this->filters;
    }

    public function setFilters($filters)
    {
        if (is_array($filters) )
        {
            foreach($filters as $filter)
            {
                $this->addFilter($filter);
            }
        }
        else
        {
            $this->addFilter($filters);
        }
    }
    
    public function getItemId() {
        return $this->itemId;
    }

    public function setItemId($itemId) {
        $this->itemId = $itemId;
    }

    public function getItemDesc() {
        return $this->itemDesc;
    }

    public function setItemDesc($itemDesc) {
        $this->itemDesc = $itemDesc;
    }
    
    public function getSearchForm() {
        return $this->searchForm;
    }

    public function setSearchForm($searchForm) {
        $this->searchForm = $searchForm;
    }
    
    public function getFrmHeight() {
        return $this->frmHeight;
    }

    public function setFrmHeight($frmHeight) {
        $this->frmHeight = $frmHeight;
    }

    public function getFrmWidth() {
        return $this->frmWidth;
    }

    public function setFrmWidth($frmWidth) {
        $this->frmWidth = $frmWidth;
    }

    public function getFrmGridColumns() {
        return $this->frmGridColumns;
    }

    public function setFrmGridColumns($frmGridColumns)
    {
        $this->frmGridColumns = $frmGridColumns;
    }

}


?>
